package com.isoftstone.bank.controller;

import com.alibaba.druid.sql.visitor.functions.Concat;
import com.isoftstone.bank.controller.base.BaseController;
import com.isoftstone.bank.service.IMerchantService;
import com.isoftstone.bank.service.impl.BOrgService;
import com.isoftstone.common.Tidings;
import com.isoftstone.merchant.entity.merchant.Merchant;
import com.isoftstone.merchant.service.payment.PaymentService;
import com.isoftstone.util.Const;
import com.isoftstone.util.DateUtil;
import com.isoftstone.util.PageData;
import com.isoftstone.util.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * Created by wlj on 2017/12/19.
 */
@Controller
public class MainPageController extends BaseController {
    @Autowired
    private BOrgService orgService;
    @Autowired
    PaymentService paymentService;
    @Autowired
    IMerchantService merchantService;
    /**
     * 数据统计：
     * 个人客户数/今日新增个人客户数
     * 缴费单位数/今日新增缴费单位数
     * 交易总数/今日新增交易数
     * 总交易金额/今日交易金额
     * @return
     */
    @RequestMapping("bank/main/statistics")
    @ResponseBody
    public  Object statistics() throws Exception {
        Tidings<Map> tidings  = new Tidings<>();

        PageData params = new PageData();
        PageData todayParams =  new PageData();
        if(null == this.getUser().getCorpId()){
            this.getUser().setCorpId("1");//管理员如果初始化未设置机构，则给最大数据权限
        }
        List<TreeNode> orgList = orgService.selectOrgTreeNodeByOrgId(Long.valueOf(this.getUser().getCorpId()));
        if(null != orgList){
            String orgIds = this.getChildrenId(orgList);
            if(null != orgIds && !"".equals(orgIds)){
                String tem = orgIds.toString();
                if(",".equals(tem.substring(tem.length()-1,tem.length()))){
                    params.put("orgIds", tem.substring(0,tem.length()-1));
                    todayParams.put("orgIds",  tem.substring(0,tem.length()-1));
                }else{
                    params.put("orgIds",  orgIds);
                    todayParams.put("orgIds",  orgIds);
                }
            }
        }
        params.put("creator",this.getUser().getUserid());
        todayParams.put("today", DateUtil.getDay());
        todayParams.put("creator",this.getUser().getUserid());

        Tidings<List<Merchant>> corpCountList= this.merchantService.getAllMerchantList(params);//缴费单位数
        Tidings<List<Merchant>> todayCorpCountList= this.merchantService.getAllMerchantList(todayParams);//今日新增缴费单位数
        String  corpCount = "0";
        String  todayCorpCount = "0";
        if(corpCountList!=null){
            corpCount = String.valueOf(corpCountList.getT().size());
        }
        if(todayCorpCountList!=null){
            todayCorpCount = String.valueOf(todayCorpCountList.getT().size());
        }

        String customersCount = this.paymentService.countBindUser(params);//个人客户数
        String todayCustomersCount = this.paymentService.countBindUser(todayParams);;//今日新增个人客户数

        String dealCount= this.paymentService.countPayment(params);//交易总数
        String todayDealCount= this.paymentService.countPayment(todayParams);//日新增交易数

        String dealSum= this.paymentService.sumPayment(params); //总交易金额
        String todayDealSum  =  this.paymentService.sumPayment(todayParams); //今日交易金额


        Map map = new HashMap<>();
        map.put("customersCount",customersCount);
        map.put("todayCustomersCount",todayCustomersCount);
        map.put("corpCount",corpCount);
        map.put("todayCorpCount",todayCorpCount);
        map.put("dealCount",dealCount);
        map.put("todayDealCount",todayDealCount);
        map.put("dealSum",dealSum);
        map.put("todayDealSum",todayDealSum);

        tidings.setT(map);
        tidings.setStatus(Const.SUCCESS);
        return tidings;
    }

    private String getChildrenId(List<TreeNode> children){
        String str = "";
        if (children != null){
            for(int i=0;i<children.size();i++){
                TreeNode temNode = children.get(i);
                str = str + temNode.getId() +",";
                if(temNode.getChildren() != null ){
                    str = str + getChildrenId(temNode.getChildren()) ;
                }
            }
        }
        return str;
    }
}
